package com.liuda066.forumssm.controller;

import com.liuda066.forumssm.common.AppResult;
import com.liuda066.forumssm.common.ResultCode;
import com.liuda066.forumssm.config.AppConfig;
import com.liuda066.forumssm.model.Article;
import com.liuda066.forumssm.model.ArticleReply;
import com.liuda066.forumssm.model.User;
import com.liuda066.forumssm.services.IArticleReplyService;
import com.liuda066.forumssm.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/9/5 22:10
 */
@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private IArticleReplyService iArticleReplyService;

    @Resource
    private IArticleService iArticleService;



    @ApiOperation("新建回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "帖子Id") @RequestParam(value = "articleId") @NonNull Long articleId,
                            @ApiParam(value = "回复内容") @RequestParam(value = "content") @NonNull String content) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION_KEY);
        if (user.getState() != 0) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString() + ",userId = " + user.getId());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        Article article = iArticleService.selectDetailById(articleId);
        if (article == null) {
            log.warn("回复的帖子不存在, articleId = " + articleId);
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(article.getUserId());

        iArticleReplyService.create(articleReply);
        return AppResult.success();
    }

    @ApiOperation("查询回复帖子")
    @GetMapping("/getReplies")
    public AppResult getRepliesByArticleId(
            @ApiParam(value = "帖子Id") @RequestParam(value = "articleId") @NonNull Long articleId) {

        Article article = iArticleService.selectDetailById(articleId);
        if (article == null ||article.getState() != 0 || article.getDeleteState() != 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }


        List<ArticleReply> articleReplies = iArticleReplyService.selectByArticleId(articleId);
        if (articleReplies == null) {
            articleReplies = new ArrayList<>();
        }
        return AppResult.success(articleReplies);
    }
}
